import React from 'react';
import { Toast,ImagePicker } from 'antd-mobile';
import { Upload } from 'antd';

export default class Index extends React.Component {
    constructor(props){
        super(props)
        this.state={
            files: [],
            img: '',
        }
    }
	componentDidMount(){
        document.title = '设置'
        this.getData()
    }
    getData(){
        global.Ajax.hhtc_request("user/setinfo",{}).then(data=>{
			if(data.code===1){
				this.setState(data.data)
			}
		})
        global.Ajax.hhtc_request("user/get_qiniu_token",{}).then(data=>{
			if(data.code===1){
				this.setState(data.data)
			}
		})
    }
    // 退出登录
    login_out(){
		localStorage.removeItem('token')
        localStorage.removeItem('uid')
        localStorage.removeItem('openid')
        this.props.history.replace('/login/login/login')
    }
    change_avatar(src){
        global.Ajax.hhtc_request("user/editavatar",{avatar: src}).then(data=>{
			if(data.code===1){
				Toast.success(data.msg,1,()=>{
                    this.getData()
                })
			}else{
                Toast.fail(data.msg,1)
            }
		})
    }
	render(){
        var that = this;
        const props = {
            name: 'file',
            action: 'https://up.qiniup.com',
            showUploadList:false,
            headers: {
                authorization: 'authorization-text',
            },
            data:(file)=>{
                console.log(file)
                var houzui = '.'+file.name.split('.')[1];
                return {
                    token:that.state.upload_token,
                    key:Date.now()+Math.floor(Math.random()*(999999-100000)+100000)+'1'+houzui,
                }   
            },
            onChange(info) {
                console.log(info)
                if (info.file.status === 'done') {
                    var src=that.state.domain+'/'+info.file.response.key
                    that.change_avatar(src)
                }else if(info.file.status === 'error') {
                    Toast.fail(info.file.response.error)
                }
            },
        };
		return(
			<div className='set_box' style={{minHeight: '100vh',background: '#F5F5F5',paddingTop: 10,}}>
                <div className='row'>
                    <p>头像</p>
                    <Upload {...props}>
                        <div>
                            <img alt='' src={this.state.avatar} className='head' />
                        </div>
                    </Upload>
                    
                    <img alt='' src={require('../../imgs/r2.png')} className='r2' />
                </div>
                <div className='row' onClick={()=>{this.props.history.push('/page/home/user/setname/'+this.state.nickname)}}>
                    <p>昵称</p>
                    <span>{this.state.nickname}</span>
                    <img alt='' src={require('../../imgs/r2.png')} className='r2' />
                </div>
                <div className='row'>
                    <p>手机号</p>
                    <span>{this.state.mobile}</span>
                </div>
                <div className='row'>
                    <p>推荐人ID</p>
                    <span>{this.state.pid}</span>
                </div>
                <div className='row' onClick={()=>{this.props.history.push('/page/home/user/editpwd')}}>
                    <p>登录密码</p>
                    <img alt='' src={require('../../imgs/r2.png')} className='r2' />
                </div>
                <p className='exit' onClick={this.login_out.bind(this)}>退出登录</p>
			</div>
		)
	}
}
